﻿module Polynom.UnitTests

open FsUnit
open FsCheck
open NUnit.Framework
open Swensen.Unquote

open FsTestProject

let toStringTestCaseSource = 
    [|
        [| "y = x" :> obj ; [0;1] :> obj |]
        [| "y = 1 + x" :> obj ; [1;1] :> obj |]
        [| "y = -2 + x" :> obj ; [-2;1] :> obj |]
        [| "y = 2 + 2 * x" :> obj ; [2;2] :> obj |]
        [| "y = x^2" :> obj ; [0;0;1] :> obj |]
        [| "y = -x^2" :> obj ; [0;0;-1] :> obj |]
        [| "y = 1 + x + x^2" :> obj ; [1;1;1] :> obj |]
        [| "y = 2 + 2 * x + 2 * x^2" :> obj ; [2;2;2] :> obj |]
        [| "y = -1 - x - x^2" :> obj ; [-1;-1;-1] :> obj |]
        [| "y = -2 - 2 * x - 2 * x^2" :> obj ; [-2;-2;-2] :> obj |]
        [| "y = x^2 + x^3" :> obj ; [0;0;1;1] :> obj |]
    |]

[<Test>]
[<TestCaseSource("toStringTestCaseSource")>]
let ``Polynom.ToString()`` s v =
    let values = v |> List.map (fun i -> double i)
    let polynomial = new Polynomial(values)
    polynomial.ToString() |> should equal s

let calcYTestCasesSource = 
    [|
        [| [0;1] :> obj; 0 :> obj; 0 :> obj |]
        [| [0;1] :> obj; 1 :> obj; 1 :> obj |]
        [| [0;1] :> obj; 2 :> obj; 2 :> obj |]
        [| [0;1] :> obj; -2 :> obj; -2 :> obj |]
        [| [0;-1] :> obj; 0 :> obj; 0 :> obj |]
        [| [0;-1] :> obj; 2 :> obj; -2 :> obj |]
        [| [0;-1] :> obj; -2 :> obj; 2 :> obj |]
        [| [2;1] :> obj; 0 :> obj; 2 :> obj |]
        [| [2;2] :> obj; 0 :> obj; 2 :> obj |]
    |]

[<Test>]
[<TestCaseSource("calcYTestCasesSource")>]
let ``Polynom.CalcY(x)`` v x y =
    let values = v |> List.map (fun i -> double i)
    let polynomial = new Polynomial(values)
    polynomial.CalcY x |> should equal (double y)

let parseTestCasesSource =
    [|
        [| "y = x" :> obj; [0;1] :> obj |]
        [| "y = x^2" :> obj; [0;0;1] :> obj |]
        [| "y = 1 + x" :> obj; [1;1] :> obj |]
        [| "y = -2 + x" :> obj; [-2;1] :> obj |]
        [| "y = -x^2" :> obj; [0;0;-1] :> obj |]
        [| "y = 1 + x + x^2" :> obj; [1;1;1] :> obj |]
        [| "y = 2 + 2 * x + 2 * x^2" :> obj; [2;2;2] :> obj |]
        [| "y = -1 - x - x^2" :> obj; [-1;-1;-1] :> obj |]
        [| "y = -2 - 2 * x - 2 * x^2" :> obj; [-2;-2;-2] :> obj |]
        [| "y = -2.0 - 2.0 * x - 2.0 * x^2" :> obj; [-2;-2;-2] :> obj |]
        [| "y = -2.0 - 2.0x - 2.0x^2" :> obj; [-2;-2;-2] :> obj |]
        [| "y = - 2.0 - 2.0 * x - 2.0 * x^2" :> obj; [-2;-2;-2] :> obj |]
        [| "y = - 2.0 - 2.0 * x- 2.0 * x^2.0" :> obj; [-2;-2;-2] :> obj |]
        [| "y = -2.0e-3 - 2.0 * x - 2.0 * x^2.0" :> obj; [-0.002;-2.;-2.] :> obj |]
        [| "y = x^2 +x^3" :> obj; [0;0;1;1] :> obj |]
    |]

[<Test>]
[<TestCaseSource("parseTestCasesSource")>]
let ``Polynom.Parse(s)`` s v =
    let polynomial = Polynomial.Parse(s)
    polynomial.Values |> should equal v